/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.sun.mail.smtp.SMTPMessage;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.mime.shim.JavaMailInternetAddress;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.XMLElement;
/*     */ import com.zimbra.common.soap.SoapHttpTransport;
/*     */ import com.zimbra.common.util.ArrayUtil;
/*     */ import com.zimbra.common.util.L10nUtil;
/*     */ import com.zimbra.common.util.L10nUtil.MsgKey;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.accesscontrol.AccessControlUtil;
/*     */ import com.zimbra.cs.httpclient.URLUtil;
/*     */ import com.zimbra.cs.util.JMSession;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.LinkedList;
/*     */ import java.util.List;
/*     */ import java.util.Locale;
/*     */ import java.util.Map;
/*     */ import java.util.concurrent.Callable;
/*     */ import java.util.concurrent.ExecutorService;
/*     */ import java.util.concurrent.Executors;
/*     */ import java.util.concurrent.Future;
/*     */ import java.util.concurrent.TimeUnit;
/*     */ import javax.mail.Message.RecipientType;
/*     */ import javax.mail.Transport;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ComputeAggregateQuotaUsage
/*     */   extends AdminDocumentHandler
/*     */ {
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  55 */     final ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  56 */     if (!AccessControlUtil.isGlobalAdmin(getAuthenticatedAccount(zsc))) {
/*  57 */       throw ServiceException.PERM_DENIED("only global admin is allowed");
/*     */     }
/*     */     
/*  60 */     Map<String, Long> domainAggrQuotaUsed = new HashMap();
/*     */     
/*  62 */     Provisioning prov = Provisioning.getInstance();
/*  63 */     List<Server> servers = prov.getAllMailClientServers();
/*     */     
/*  65 */     ExecutorService executor = Executors.newFixedThreadPool(LC.compute_aggregate_quota_threads.intValue());
/*  66 */     List<Future<Map<String, Long>>> futures = new LinkedList();
/*  67 */     for (final Server server : servers) {
/*  68 */       futures.add(executor.submit(new Callable()
/*     */       {
/*     */         public Map<String, Long> call() throws Exception
/*     */         {
/*  72 */           ZimbraLog.misc.debug("Invoking %s on server %s", new Object[] { "GetAggregateQuotaUsageOnServerRequest", server.getName() });
/*     */           
/*     */ 
/*  75 */           Element req = new Element.XMLElement(AdminConstants.GET_AGGR_QUOTA_USAGE_ON_SERVER_REQUEST);
/*  76 */           String adminUrl = URLUtil.getAdminURL(server, "/service/admin/soap/");
/*  77 */           SoapHttpTransport mTransport = new SoapHttpTransport(adminUrl);
/*  78 */           mTransport.setAuthToken(zsc.getRawAuthToken());
/*     */           Element resp;
/*     */           try {
/*  81 */             resp = mTransport.invoke(req);
/*     */           } catch (Exception e) {
/*  83 */             throw new Exception("Error in invoking GetAggregateQuotaUsageOnServerRequest on server " + server.getName(), e);
/*     */           }
/*     */           
/*     */ 
/*  87 */           List<Element> domainElts = resp.getPathElementList(new String[] { "domain" });
/*  88 */           Map<String, Long> retMap = new HashMap();
/*  89 */           for (Element domainElt : domainElts) {
/*  90 */             retMap.put(domainElt.getAttribute("id"), Long.valueOf(domainElt.getAttributeLong("used")));
/*     */           }
/*     */           
/*  93 */           return retMap;
/*     */         }
/*     */       }));
/*     */     }
/*  97 */     shutdownAndAwaitTermination(executor);
/*     */     
/*     */ 
/* 100 */     for (Future<Map<String, Long>> future : futures)
/*     */     {
/*     */       try {
/* 103 */         result = (Map)future.get();
/*     */       } catch (Exception e) {
/* 105 */         throw ServiceException.FAILURE("Error in getting task execution result", e);
/*     */       }
/* 107 */       for (String domainId : result.keySet()) {
/* 108 */         Long delta = (Long)result.get(domainId);
/* 109 */         Long aggr = (Long)domainAggrQuotaUsed.get(domainId);
/* 110 */         domainAggrQuotaUsed.put(domainId, Long.valueOf(aggr == null ? delta.longValue() : aggr.longValue() + delta.longValue()));
/*     */       }
/*     */     }
/*     */     Map<String, Long> result;
/* 114 */     Element response = zsc.createElement(AdminConstants.COMPUTE_AGGR_QUOTA_USAGE_RESPONSE);
/* 115 */     ExecutorService sendWarnMsgExecutor = Executors.newSingleThreadExecutor();
/* 116 */     for (String domainId : domainAggrQuotaUsed.keySet()) {
/* 117 */       Domain domain = prov.getDomainById(domainId);
/* 118 */       Long used = (Long)domainAggrQuotaUsed.get(domainId);
/* 119 */       domain.setAggregateQuotaLastUsage(used.longValue());
/* 120 */       Long max = Long.valueOf(domain.getDomainAggregateQuota());
/* 121 */       if ((max.longValue() != 0L) && (used.longValue() * 100L / max.longValue() > domain.getDomainAggregateQuotaWarnPercent())) {
/* 122 */         sendWarnMsg(domain, sendWarnMsgExecutor);
/*     */       }
/* 124 */       Element domainElt = response.addElement("domain");
/* 125 */       domainElt.addAttribute("name", domain.getName());
/* 126 */       domainElt.addAttribute("id", domainId);
/* 127 */       domainElt.addAttribute("used", used);
/*     */     }
/* 129 */     sendWarnMsgExecutor.shutdown();
/* 130 */     return response;
/*     */   }
/*     */   
/*     */   private void sendWarnMsg(final Domain domain, ExecutorService executor) {
/* 134 */     final String[] recipients = domain.getDomainAggregateQuotaWarnEmailRecipient();
/* 135 */     if (ArrayUtil.isEmpty(recipients)) {
/* 136 */       return;
/*     */     }
/* 138 */     executor.execute(new Runnable()
/*     */     {
/*     */       public void run()
/*     */       {
/*     */         try {
/* 143 */           SMTPMessage out = new SMTPMessage(JMSession.getSmtpSession());
/*     */           
/*     */ 
/* 146 */           out.setFrom(new JavaMailInternetAddress("Postmaster <postmaster@" + domain.getName() + ">"));
/*     */           
/* 148 */           for (String recipient : recipients) {
/* 149 */             out.setRecipient(Message.RecipientType.TO, new JavaMailInternetAddress(recipient));
/*     */           }
/*     */           
/* 152 */           out.setSentDate(new Date());
/*     */           
/*     */ 
/* 155 */           Locale locale = Locale.getDefault();
/* 156 */           out.setSubject(L10nUtil.getMessage(L10nUtil.MsgKey.domainAggrQuotaWarnMsgSubject, locale, new Object[0]));
/*     */           
/* 158 */           out.setText(L10nUtil.getMessage(L10nUtil.MsgKey.domainAggrQuotaWarnMsgBody, locale, new Object[] { domain.getName(), Double.valueOf(domain.getAggregateQuotaLastUsage() / 1024.0D / 1024.0D), Integer.valueOf(domain.getDomainAggregateQuotaWarnPercent()), Double.valueOf(domain.getDomainAggregateQuota() / 1024.0D / 1024.0D) }));
/*     */           
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 164 */           Transport.send(out);
/*     */         } catch (Exception e) {
/* 166 */           ZimbraLog.misc.warn("Error in sending aggregate quota warning msg for domain %s", domain.getName(), e);
/*     */         }
/*     */       }
/*     */     });
/*     */   }
/*     */   
/*     */   private static void shutdownAndAwaitTermination(ExecutorService executor) throws ServiceException
/*     */   {
/* 174 */     executor.shutdown();
/*     */     
/*     */     try
/*     */     {
/* 178 */       if (!executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS)) {
/* 179 */         throw ServiceException.FAILURE("Time out waiting for GetAggregateQuotaUsageOnServerRequest result", null);
/*     */       }
/*     */     }
/*     */     catch (InterruptedException ie) {
/* 183 */       executor.shutdownNow();
/*     */       
/* 185 */       Thread.currentThread().interrupt();
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/ComputeAggregateQuotaUsage.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */